package com.sean.reverse.util;

import com.github.qcloudsms.SmsResultBase;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.httpclient.HTTPException;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author sean
 * @createTime 2022/4/14 22:01
 * @description
 */
@Data
@Component
@ConfigurationProperties("spring.cloud.tencent")
public class SmsComponent {

    String appid;//应用id
    String appkey;//应用密码
    String templateid;//短信模板id
    String smssign;//签名内容

    /**
     * 按模板发送短信 支持单发
     */
    public void sendMessage(String court, String timeSlot, String phone){
        String appId = this.appid;//应用id
        String appKey = this.appkey;//应用密码
        String templateId = this.templateid;//短信模板id
        String smsSign = this.smssign;//签名内容

//        System.out.println(appId+"\n"+appKey+"\n"+templateId+"\n"+smsSign);

        String[] param = new String[]{court,timeSlot};
        //验证手机号
        String regExp = "^((13[0-9])|(14[5,7,9])|(15[0-3,5-9])|(166)|(17[3,5,6,7,8])" +
                "|(18[0-9])|(19[8,9]))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(phone);
        if(!m.matches()){
            phone = "19800338191";
        }

        try {
            // 单发
            SmsSingleSender sender = new SmsSingleSender(Integer.parseInt(appId), appKey);
            SmsResultBase result = sender.sendWithParam("86", phone,
                    Integer.parseInt(templateId), param, smsSign, "", "");

            System.out.println(result);
        } catch (HTTPException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
